Dependabot の PR を GitHub Actions で yarn-deduplicate する
やりたいこと
Dependabot に Yarn の依存関係の更新を任せているが、依存関係の重複の排除はやってくれない。
なので、GitHub Actions を利用して自動化したい。
参考
Deduplicate Yarn Dependencies · Issue #5830 · dependabot/dependabot-core
GitHub Actions で変更があるときだけ git commit & push する - Zenn
GitHub Action で PR に何かして push する - 宇宙行きたい
作成したワークフロー
NOTE
以下のような yaml を用意しましたが、結局テスト用のワークフローで dedupe -> commit -> push をやることにしました。
なので、デバッグしていません。ご参考まで。
code: .github/workflows/dependabot-dedupe.yml
name: Dedupe dependencies for Dependabot
on:
push:
branches: 'dependabot/**'
permissions:
contents: write
jobs:
dedupe:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.ref_name }} # あっているか怪しい
# ref: ${{ github.event.pull_request.head.ref }} # PRをトリガーにするならこっち
- uses: actions/setup-node@v4
# 必要に応じて yarn dedupe, pnpm dedupe に置き換える
- run: npx -y yarn-deduplicate # いちいち yarn install すると時間がかかるので
- run: |
LOCKFILE_PATH=./yarn.lock
# lockfile に差分があれば、bot として commit して push する
if ! git diff --exit-code --quiet --relative $LOCKFILE_PATH; then
git config user.name 'github-actionsbot'
git config user.email 'github-actionsbot@users.noreply.github.com'
git add $LOCKFILE_PATH
git commit -m "chore(deps): Dedupe dependencies" -m "dependabot skip"
git push
fi
実際に運用しているワークフローでは以下のようなステップにしています。
npx -y yarn-deduplicate && yarn install --frozen-lockfile
-> git add yarn.lock && git commit -m "dedupe" && git push
-> yarn build
-> yarn test
-> ...
CIで npx yarn-deduplicate --list --fail で落ちるとストレスなので、いっそのことcommitしたれ、という思想です。
あとがき
余力ができたら Yarn v1 をやめて pnpm に移行したいなぁ (移行した✌)
#技術系_TIPS #2024-02